package render.tree
{
	import mx.collections.ICollectionView;

	public class PageControl implements INode
	{
		private var parent:CategoryNode;
		private var pageSize:int;
		private var uri:String;
		private var startIndex:int;
		private var lastSize:int;
		
		private var _pageUpable:Boolean;
		private var _pageDownable:Boolean;
		
		public function PageControl(parent:CategoryNode,uri:String,startIndex:int,pageSize:int)
		{
			this.parent = parent;
			this.uri = uri;
			this.startIndex = startIndex;
			this.pageSize = pageSize;
			this.lastSize = pageSize + 1;
		}

		public function getChildren():ICollectionView
		{
			return null;
		}
		
		public function getChildAt(index:int):INode
		{
			return null;
		}
		
		public function getChildrenAmount():int
		{
			return 0;
		}
		
		public function getNodeType():String
		{
			return StructureDescriptor.NODE_TYPE_PAGECONTORL;
		}
		
		public function getURI():String
		{
			return null;
		}
		
		public function fetch():void
		{
		}
		public function getPageInfo():String{
			return "The " + startIndex + " to " + (startIndex + pageSize - 1 ) + " item";			
		}
		public function get pageUpable():Boolean
		{	
			return startIndex > 0;
		}
		public function get pageDownable():Boolean
		{
			return lastSize > pageSize;
		}
		public function pageUp():void
		{
			parent.clear();
			
			parent.fetchItems(pageUpHandler,uri,startIndex-pageSize,pageSize);
		}
		private function pageUpHandler(array:Array):void
		{
			lastSize = array.length;
			startIndex-= pageSize;
			
			parent.pushItems(array);
			parent.sort();	
					
			parent.pushItem(this);
		}
		public function pageDown():void
		{
			parent.clear();
			parent.fetchItems(pageDownHandler,uri,startIndex+pageSize,pageSize);
		}
		private function pageDownHandler(array:Array):void
		{
			lastSize = array.length;
			startIndex+=pageSize;
			
			parent.pushItems(array);
			parent.sort();
			
			parent.pushItem(this);
		}
	}
}